// pages/doctor-validation-plan/doctor-validation-plan.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    localImg: app.globalData.staticimgUrl,  // 本地图片
    data:{},
    cityRank:{},     // 医院级别
    anaesthesia:{},  // 麻醉级别
    totalPeice:'',
    houtai:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const information = wx.getStorageSync('locationData');
    console.log('本地储存',information);
    console.log('后台需要',JSON.parse(options.data))
    this.setData({
      data: JSON.parse(options.data),
      information
    })
    this.getCityRankList();
    this.childTypeData();
   
  },
  // 子类产品数据
  childTypeData(){

    const houtai = JSON.parse(this.data.data.relationInfos);
    const bendi = this.data.information.relationProductList;

    for (let i = 0; i < houtai.length;i++){
      for (let k = 0; k < bendi.length; k++) {
        for (let j = 0; j < bendi[k].relationProductPriceList.length;j++) {
          if (bendi[k].relationProductPriceList[j].id == houtai[i].relationPriceId){
            houtai[i]["typeName"] = bendi[k].relationProductPriceList[j].typeName;
            houtai[i]["thumbnail"] = bendi[k].thumbnail;
            houtai[i]["relationName"] = bendi[k].relationName;
          }
        }
      }
    }
    console.log(houtai)
    this.setData({
      houtai
    })
  },

  // 获取场地列表
  getCityRankList() {
    const that = this;
    const cityRankId = this.data.data.cityRankId;
    const param = {
      areaId: 2
    };
    app.request({
      url: "doctor/demandApi/getCityRankList",
      data: param,
      success: function (res) {
        const data= res.data;
        for(let i = 0; i<data.length; i++){
          if(data[i].id == cityRankId){
            that.setData({
              cityRank: res.data[i]
            })
          }
        }
        that.anaesthesia();
      },
      fail: function (res) {
        // app.requestFail(res);
      }
    });
  },
  // 麻醉
  anaesthesia(){
    const that = this;
    const hocusRankId = this.data.data.hocusRankId ;
    app.request({
      url: "shopp/serviceGoodsMouldApi/getArrayData",
      method: "GET",
      success: function (res) {
        const data = res.data;
        for(let i =0; i<data.length; i++){
          if(data[i].id == hocusRankId){
            that.setData({
              anaesthesia:res.data[i]
            })
           
          }
        }
        that.totalPeice();
      },
      fail: function (res) {
        // app.requestFail(res);
      }
    });
  },
  // 方案总价
  totalPeice(){
    // 主价
    const mainPrice = this.data.data.mainPrice;
    // 场所价格
    const cityRankPrice = this.data.cityRank.price;
    // 麻醉价格
    const anaesthesiaPrice = this.data.anaesthesia.price;

    const childData = this.data.houtai;

    let child =0;

    for (let i = 0; i < childData.length; i++){
      child += parseFloat(childData[i].relationPrice)
    }
    const totalPeice = parseFloat(mainPrice) + parseFloat(cityRankPrice) + parseFloat(anaesthesiaPrice) + parseFloat(child);
    this.setData({
      totalPeice: totalPeice.toFixed(2)
    })
  },

  // 生成方案
  createScheme(){
    console.log("success")
    const param = this.data.data;
    app.request({
      url: "doctor/demandApi/createScheme",
      data: param,
      success: function (res) {
        wx.showModal({
          title: '提示消息',
          content: res.message,
          showCancel: false,
          success(res) {
            wx.removeStorageSync('locationData')

            if (res.confirm) {
              wx.switchTab({
                url: '/pages/doctor-index/doctor-index',
              })
            }
          }
        })
      },
      fail: function (res) {
        // app.requestFail(res);
      }
    });
  }
})